package com.example.goods_manage.service.impl;

import com.example.goods_manage.mapper.CategoryMapper;
import com.example.goods_manage.service.CategoryService;
import com.example.goods_manage.utils.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/5/30:13:37
 */

@SuppressWarnings({"unchecked", ""})
@Service
public class CategoryServiceImpl implements CategoryService {
    private static final Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Resource
    public CategoryMapper categoryMapper;


    @Override
    public AjaxResult statisticsCategory() {
        List<Map<String, Object>> sta = categoryMapper.statisticsCategory();
        //获取所有的名称 封装成list
        List<String> nameList = sta
                .stream()
                .filter(map-> !ObjectUtils.isEmpty(map.get("name")))
                .map(v -> v.get("name").toString())
                .collect(Collectors.toList());
        List<String> list = new ArrayList<>();
        for (Map<String, Object> map : sta) {
            if(ObjectUtils.isEmpty(map.get("name"))){
                continue;
            }
            String name = map.get("name").toString();
            list.add(name);
        }
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("data",sta);
        hashMap.put("categoryData",nameList);
        return AjaxResult.success(hashMap);
    }

    @Override
    public List getAllCategory() {
        List<Map<String, Object>> allCategory = categoryMapper.getAllCategory();
        return allCategory;
    }
}
